package h0904;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 行星碰撞
 * @date 2024/9/4 20:26
 */
public class PlanetCollision {
    public static void main(String[] args) {
        int[] asteroids = {5,10,-5};
        int[] res = asteroidCollision(asteroids);
        System.out.println(Arrays.toString(res));
    }

    public static int[] asteroidCollision(int[] asteroids) {
        LinkedList<Integer> stack = new LinkedList<>();
        for (int i = 0; i < asteroids.length; i++) {
            boolean flag=true;
            while (!stack.isEmpty() && stack.peekLast() > 0 && asteroids[i] < 0) {
                int top = stack.peekLast();
                int cur = asteroids[i];
                if (top < -cur) {
                    stack.removeLast();
                    continue;
                } else if (top==-cur){
                    stack.removeLast();
                }
                flag=false;
                break;
            }
            if (flag) {
                stack.addLast(asteroids[i]);
            }
        }
        return stack.stream().mapToInt(Integer::intValue).toArray();
    }
}
